package com.zyb.warehouse.service.impl;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.InStore;
import com.zyb.warehouse.mapper.InStoreMapper;
import com.zyb.warehouse.mapper.ProductMapper;
import com.zyb.warehouse.service.InStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:入库单业务层接口实现类
 */
@Service
public class InStoreServiceImpl implements InStoreService {

    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 分页查询入库单
     *
     * @param page
     * @param inStore
     * @return
     */
    public Page pageInStore(Page page, InStore inStore) {
        //查询总记录数
        Integer count = inStoreMapper.selectInStoreTotal(inStore);
        //查询所有入库单带分页
        List<InStore> inStoreList = inStoreMapper.pageInStore(page, inStore);
        //属性拷贝
        page.setTotalNum(count);
        page.setResultList(inStoreList);
        return page;
    }

    /**
     * 确认入库
     *
     * @param inStore
     * @return
     */
    @Transactional
    public Result confirmInStore(InStore inStore) {
        //先修改入库单状态为已入库
        Integer i = inStoreMapper.updateIsInState(inStore.getInsId());
        if (i > 0) {
            //把入库的实际数量和原来的库存相加
            Integer j = productMapper.updateProductInventById(inStore.getProductId(), inStore.getInNum());
            if (j > 0) {
                return Result.ok("入库成功");
            }
            return Result.err(Result.CODE_ERR_BUSINESS, "入库失败");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "入库失败");
    }
}
